import axios from "axios";
import { toast } from "../utils/index.js";
import { getCookie } from "../utils/auth.js";
const req = axios.create({
  baseURL: "/api",
});

// 添加请求拦截器
req.interceptors.request.use(
  function (config) {
    const token = getCookie();
    if (token) {
      config.headers["token"] = token;
    }
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);

req.interceptors.response.use(
  function (response) {
    return response.data.data;
  },
  function (error) {
    toast(error.response.data.msg, "error");
    return Promise.reject(error);
  }
);
export default req;
